package com.sike.controller;

import com.sike.entity.Mail;
import com.sike.service.MailService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;


import java.io.File;
import java.io.IOException;

@Controller
public class MailController {
    @Autowired
    MailService mailService;

    @ResponseBody
    @PostMapping("/sendSimpleMail")
    public String sendSimpleMail(Mail mail){
        mailService.sendSimpleMailMessage(mail);
        return "简单邮件发送成功！";
    }


    @ResponseBody
    @PostMapping("/sendMailWithFile")
    public String sendMailWithFile(Mail mail, MultipartFile uploadfile, HttpServletRequest request) throws IOException {
        //存储上传文件的目标文件夹(服务器上的，每个人的路径都不同，按个人实际情况设置)
        String path="C:\\springbootbook\\mail1\\target\\classes\\static\\upload";
        String filename=uploadfile.getOriginalFilename(); //获取上传的文件名称
        File file=new File(path,filename); //构造上传目标File
        uploadfile.transferTo(file);//上传文件
        mail.setFile(file); //将上传文件设置到要发送的邮件的附件
        mailService.sendMailWithFile(mail);
        return "带附件邮件发送成功！";
    }


    @ResponseBody
    @PostMapping("/sendTemplateMail")
    public String sendTemplateMail(Mail mail,String username,String orderNo,String amount,String mobile){
        mailService.sendTempateMail(mail,username,orderNo,amount,mobile);
        return "模板邮件发送成功！";
    }


}

